package com.leyou.item.controller;

import com.leyou.common.pojo.PageResult;
import com.leyou.item.pojo.Brand;
import com.leyou.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("brand")
public class BrandsController {

    @Autowired
    private BrandService brandService;

    /**
     * 分页查询品牌，把查询到的所有数据信息封装到pageresult中
     * 分页查询最起码要告诉服务器现在查询在哪一页，现在的页容量多少个
     *
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("page")
    public ResponseEntity<PageResult<Brand>> page(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                  @RequestParam(value = "rows", defaultValue = "10 ") Integer rows,
                                                  @RequestParam(value = "sortBy", required = false) String sortBy,
                                                  @RequestParam(value = "desc", required = false) Boolean desc,
                                                  @RequestParam(value = "key", required = false) String key
    ) {
        PageResult<Brand> pageResult = brandService.page(page, rows, sortBy, desc, key);
        if (pageResult != null && null != pageResult.getItems() && 0 != pageResult.getItems().size()) {
            return ResponseEntity.ok(pageResult);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }

    /**
     * 保存品牌，并同时获取品牌对应的分类的id，由于springMVC的消息转换器可以自动的把有特征的字符串转换成集合数组，所以直接转换
     *
     * @param brand
     * @param cids
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> addBrand(Brand brand, @RequestParam("cids") List<Long> cids) {

        brandService.addBrand(brand, cids);

        return ResponseEntity.status(HttpStatus.CREATED).build();//返回201表示新增注册完成
    }

    @PutMapping
    public ResponseEntity<Void> updateBrand(Brand brand, @RequestParam("cids") List<Long> cids) {

        brandService.updateBrand(brand, cids);

        return ResponseEntity.status(HttpStatus.CREATED).build();//返回201表示新增注册完成
    }

    @GetMapping("cid/{cid}")
    public ResponseEntity<List<Brand>> queryBrandByCategory(@PathVariable("cid") Long cid) {

        List<Brand> brands = brandService.queryBrandByCategory(cid);
        if (brands != null && 0 != brands.size()) {
            return ResponseEntity.ok(brands);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 根据品牌的id查询品牌信息
     *
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<Brand> queryBrandById(@PathVariable("bid") Long bid) {

        Brand brand = brandService.queryBrandById(bid);
        if (brand != null) {
            return ResponseEntity.ok(brand);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 一次查询多个品牌信息
     *
     * @param bids
     * @return
     */
    @GetMapping
    public ResponseEntity<List<Brand>> queryBrandByIds(@RequestParam("bids") List<Long> bids) {

        List<Brand> brands = brandService.queryBrandByIds(bids);
        if (brands != null && 0 != brands.size()) {
            return ResponseEntity.ok(brands);
        }
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

}
